# Copyright (c) 2023 Presto Labs Pte. Ltd.
# Author: taekwon

import os
import sys
import subprocess
import time
import re
import getpass
from datetime import datetime
from os.path import expanduser
import tempfile

import requests
from absl import app, flags

from coin.exchange.upbit_v1.kr_rest.native_private_client import UpbitNativePrivateClient


class MySpotClient(UpbitNativePrivateClient):
  def __init__(self, key_file):
    super().__init__(key_file=key_file)


def init_flags():
  flags.DEFINE_string('account', 'chpark0', 'account name')


def print_balance(spot_npc):
  bal = spot_npc.query_GET_accounts().msg
  print("########################## BALANCE ##########################")
  for ass in bal:
    print(f"{ass['currency']:<10}: {ass['balance']:>20}, locked: {ass['locked']:>20}")


def main(_):
  account = flags.FLAGS.account
  username = getpass.getuser()
  key_file = f"/tmp/temp_key_{username}.json"
  cmd = f"""./python/coin_deploy/fab.sh \
-f python/coin_deploy/fabhelper/fabutil.py \
-P upload_coin_key_high:vault_token_file="./python/coin_deploy/vault/coin_key_high.json",\
key_name="trade/{account}/upbit/trade_key.json",\
remote_key_path="{key_file}",\
run_local=True
"""
  output = subprocess.check_output(cmd, shell=True)
  msc = MySpotClient(key_file=key_file)
  print_balance(msc)


if __name__ == '__main__':
  init_flags()
  app.run(main)

# Usage
# ./pyrunner python/experimental/taekwon/upbit_balance_check.py --account=chpark0
